package com.oocl.euc.audit;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.oocl.euc.core.maintenance.model.Users;
import com.oocl.euc.framework.base.persistence.oracle.SequenceIDPersistable;
import com.oocl.euc.framework.common.deserializer.JsonUtilDateDeserializer;
import com.oocl.euc.framework.common.deserializer.JsonUtilDateSerializer;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.util.Date;

@MappedSuperclass
@EntityListeners(value = {AuditingEntityListener.class})
public class AuditEntity extends SequenceIDPersistable {

    @JoinColumn(name = "CREATED_BY", referencedColumnName = "STAFF_ID", nullable = false, updatable = false)
    @CreatedBy
    @ManyToOne
    @JsonIgnoreProperties(value = {
            "displayName",
            "gender",
            "email",
            "jobTitle",
            "office",
            "officeCode",
            "phone",
            "fax",
            "country",
            "department",
            "section1",
            "section2",
            "address",
            "location",
            "firstName",
            "middleName",
            "lastName",
            "active",
            "needSync",
            "updatedAt",
            "createdAt"
    }, ignoreUnknown = true)
    private Users createdBy;

    @Temporal(TemporalType.TIMESTAMP)
    @JsonSerialize(using = JsonUtilDateSerializer.class)
    @JsonDeserialize(using = JsonUtilDateDeserializer.class)
    @Column(name = "CREATED_AT", updatable = false, nullable = false)
    @CreatedDate
    private Date createdAt;

    @JoinColumn(name = "UPDATED_BY", referencedColumnName = "STAFF_ID", nullable = false)
    @LastModifiedBy
    @ManyToOne
    @JsonIgnoreProperties(value = {
            "displayName",
            "gender",
            "email",
            "jobTitle",
            "office",
            "officeCode",
            "phone",
            "fax",
            "country",
            "department",
            "section1",
            "section2",
            "address",
            "location",
            "firstName",
            "middleName",
            "lastName",
            "active",
            "needSync",
            "updatedAt",
            "createdAt"
    }, ignoreUnknown = true)
    private Users updatedBy;

    @Temporal(TemporalType.TIMESTAMP)
    @JsonSerialize(using = JsonUtilDateSerializer.class)
    @JsonDeserialize(using = JsonUtilDateDeserializer.class)
    @Column(name = "UPDATED_AT", nullable = false)
    @LastModifiedDate
    private Date updatedAt;

    public Date getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    public Date getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(Date updatedAt) {
        this.updatedAt = updatedAt;
    }

    public Users getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(Users createdBy) {
        this.createdBy = createdBy;
    }

    public Users getUpdatedBy() {
        return updatedBy;
    }

    public void setUpdatedBy(Users updatedBy) {
        this.updatedBy = updatedBy;
    }
}
